package com.lzx.iteam;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.*;
import android.util.Log;
import android.view.*;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import com.lzx.iteam.adapter.TalkListAdapter;
import com.lzx.iteam.bean.TalkListData;
import com.lzx.iteam.emotion.EmotionUtil;
import com.lzx.iteam.net.*;
import com.lzx.iteam.util.*;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人脉圈 and 个人空间
 * Created by Megan on 15/10/12.
 */
public class TalkListActivity extends Activity implements View.OnClickListener, View.OnTouchListener, AbsListView.OnScrollListener {

    private LinearLayout mLlBack, mLlMore,mLlHelp, mLlBottomLayout, mEmotionLayout, mLlEmotionDots, mLlNoData;
    private ImageView mIvNoData;
    private TextView mTvNoData;
    private RelativeLayout mRlTalkList;
    private ListView mLvTalkList;
    private EditText mEtCommentEdit;
    private ImageView mIvEmotiom, mRightImg;
    private ViewPager mVpEmotion;
    private TextView mNewMessage, mTvTitle, mTvSend, mFooterText;
    private LinearLayout mFooterLayout;
    private ProgressBar mFooterProgress;
    private TalkListAdapter mTalkListAdapter;
    public static String MY_PERSONAL_SPACE = "my_personal_space";
    public static String OTHER_PERSONAL_SPACE = "other_personal_space";
    public static String CONTACTS_CIRCLE = "contacts_circle";
    private int pageIndex = 0;
    private AllDialogUtil mDialogUtil;
    private Dialog mWaitDialog;
    private ArrayList<TalkListData> talkListTempDatas = new ArrayList<TalkListData>();
    private TalkListCenter mCenter;
    private String mAction;
    boolean is_scrolling = false;//是否还在滚动
    private PreferenceUtil mPreferenceUtil;
    private Map<String, String> mReplyMap;
    private String mSendMessage;
    private String mDeleteTalkId;
    private int mPosition;
    private TalkListActivity mActivity;
    public  TalkListActivity instance = null;
    private EmotionUtil mEmotionUtil;


    private final int DELETE_TALK = 1001;
    private final int GET_TALK_NEW_MSG_COUNT = 1002;
    private final int ADD_COMMENT = 1003;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case DELETE_TALK:
                    /*if (mWaitLayout.getVisibility() == View.VISIBLE){
                        mWaitLayout.setVisibility(View.GONE);
                    }*/
                    httpReqDlgDismiss();
                    if (msg.arg1 == 0) {
                        talkListTempDatas.remove(mPosition);
                        mCenter.deleteTalkById(mDeleteTalkId);
                        mTalkListAdapter.bindData(talkListTempDatas);
//                        mCenter.upDateCloudTalkData(false, null, talkListTempDatas.get(mPosition).comments, talkListTempDatas.get(mPosition).talkId);
                    }else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(TalkListActivity.this, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(TalkListActivity.this, (String) msg.obj,
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                case GET_TALK_NEW_MSG_COUNT:
                    if (msg.arg1 == 0) {
                        TalkListData talkListData = (TalkListData) msg.obj;
                        if (talkListData != null && Integer.parseInt(talkListData.msgCount) > 0) {
                            mNewMessage.setVisibility(View.VISIBLE);
                            mNewMessage.setText("有" + talkListData.msgCount + "条关于你的消息");

                            AlphaAnimation alp = new AlphaAnimation(1.0f, 0.0f);
                            alp.setDuration(3000);
                            mNewMessage.setAnimation(alp);
                            alp.setAnimationListener(new AnimationListener() {
                                public void onAnimationStart(Animation animation) {
                                }

                                public void onAnimationRepeat(Animation animation) {
                                }

                                public void onAnimationEnd(Animation animation) {
                                    mNewMessage.setVisibility(View.INVISIBLE);
                                }
                            });

                        } else {
                            mNewMessage.setVisibility(View.GONE);

                        }
                    }

                    break;
                case ADD_COMMENT:
                    if (msg.arg1 == 0) {
                        String talkAboutId = (String) msg.obj;
                        mReplyMap.put(Constants.TALK_ABOUT_ID, talkAboutId);
                        mReplyMap.put(Constants.TALK_COMMENT, mSendMessage);
                        if (talkListTempDatas.get(mPosition).comments == null) {
                            ArrayList<Map<String, String>> comments = new ArrayList<Map<String, String>>();
                            comments.add(mReplyMap);
                            talkListTempDatas.get(mPosition).comments = comments;
                        } else {
                            talkListTempDatas.get(mPosition).comments.add(mReplyMap);
                        }
                        mTalkListAdapter.bindData(talkListTempDatas);
                        mCenter.upDateCloudTalkData(false, null, talkListTempDatas.get(mPosition).comments, talkListTempDatas.get(mPosition).talkId);
                        mLlBottomLayout.setVisibility(View.GONE);
                        mSendMessage = "";
                        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(TalkListActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(TalkListActivity.this, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(TalkListActivity.this, (String) msg.obj,
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_list_layout);
        mCenter = TalkListCenter.getInstance(this);
        instance = this;
        mActivity = this;
        initView();
        initEmotion();
        getIntentData();
        GetNewMessage();
    }


    private void initView() {
        mDialogUtil = new AllDialogUtil(this);
        mPreferenceUtil = PreferenceUtil.getInstance(this);
        mLlBack = (LinearLayout) findViewById(R.id.talk_list_back);
        mLlMore = (LinearLayout) findViewById(R.id.talk_list_more);
        mLlHelp= (LinearLayout) findViewById(R.id.talk_list_ll_help);
        mLlNoData = (LinearLayout) findViewById(R.id.ll_talk_no_data);
        mIvNoData = (ImageView) findViewById(R.id.iv_talk_no_data);
        mTvNoData = (TextView) findViewById(R.id.tv_talk_no_data);
        mRlTalkList = (RelativeLayout) findViewById(R.id.rl_talk_list_layout);
        mRightImg = (ImageView) findViewById(R.id.iv_talk_list_more_image);
        mLvTalkList = (ListView) findViewById(R.id.lv_talk_list);
        mLlBottomLayout = (LinearLayout) findViewById(R.id.ll_talk_list_bottom);
        mEtCommentEdit = (EditText) findViewById(R.id.et_talk_list_input);
        mIvEmotiom = (ImageView) findViewById(R.id.iv_talk_list_emotion);
        mEmotionLayout = (LinearLayout) findViewById(R.id.ll_talk_list_emotion);
        mVpEmotion = (ViewPager) findViewById(R.id.talk_list_emotion_viewpager);
        mNewMessage = (TextView) findViewById(R.id.tv_list_new_aboutme);
        mTvSend = (TextView) findViewById(R.id.tv_talk_list_send);
        mTvTitle = (TextView) findViewById(R.id.tv_talk_list_title);
        mLlEmotionDots = (LinearLayout) findViewById(R.id.talk_list_emotion_dots_container);
        mEtCommentEdit.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        mNewMessage.setOnClickListener(this);
        mLlBack.setOnClickListener(this);
        mIvEmotiom.setOnClickListener(this);
        mLlMore.setOnClickListener(this);
        View footerView = LayoutInflater.from(this).inflate(R.layout.xlistview_footer, null);
        mFooterLayout = (LinearLayout) footerView.findViewById(R.id.xlistview_footer_content);
        mFooterText = (TextView) footerView.findViewById(R.id.xlistview_footer_hint_textview);
        mFooterProgress = (ProgressBar) footerView.findViewById(R.id.xlistview_footer_progressbar);
        mFooterLayout.setVisibility(View.GONE);
        mLvTalkList.addFooterView(footerView);
        mLvTalkList.setOnTouchListener(this);
        mLvTalkList.setOnScrollListener(this);
        mEtCommentEdit.setOnClickListener(this);
        mTvSend.setOnClickListener(this);
        mLlHelp.setOnClickListener(this);
        mEtCommentEdit.addTextChangedListener(mTextWatcher);

//        mWaitLayout = (LinearLayout) findViewById(R.id.wait_layout);
/*
        mEtCommentEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEND) {
                    Toast.makeText(TalkListActivity.this, "处理发送的操作", Toast.LENGTH_SHORT).show();
                }

                return false;
            }
        });
*/
        mLvTalkList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == mTalkListAdapter.getCount()) {
                    return;
                } else {
//                    Intent intent = new Intent(TalkListActivity.this, ContactsTalkDetailActivity.class);
//                    intent.putExtra("talkListData", (TalkListData) mTalkListAdapter.getItem(i - 1));
//                    intent.putExtra("talk_id", ((TalkListData) mTalkListAdapter.getItem(i - 1)).talkId);
//                    startActivity(intent);
                }
            }
        });
        mLvTalkList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == mTalkListAdapter.getCount()) {
                } else {
                    if (mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1").equals(((TalkListData) mTalkListAdapter.getItem(i)).uId)) {
                        mPosition = i;
                        mDeleteTalkId = ((TalkListData) mTalkListAdapter.getItem(i)).talkId;
                        showDeleteTalkDlg(mDeleteTalkId);
                    }
                }
                return false;
            }
        });

//        mLvTalkList.setonRefreshListener(new PullListView.OnRefreshListener() {
//            @Override
//            public void onRefresh(boolean isTop) {
//                if (isTop) {
//                    initData(pageIndex, true);
//                    mLvTalkList.onRefreshComplete();
//                } else {
//                    initData(++pageIndex, false);
//                    mLvTalkList.onRefreshComplete();
//                }
//            }
//        });
        mCenter.setUpdateTalkListener(new TalkListCenter.TalkDataListener() {

            @Override
            public void onChange(ArrayList<TalkListData> talkList) {
                talkListTempDatas.clear();
                talkListTempDatas.addAll(talkList);
                if (talkListTempDatas.size() > 0) {
                    mLlNoData.setVisibility(View.GONE);
                    mRlTalkList.setVisibility(View.VISIBLE);
                } else {
                    mLlNoData.setVisibility(View.VISIBLE);
                    mRlTalkList.setVisibility(View.GONE);
                }
                mTalkListAdapter.bindData(talkListTempDatas);
                Log.d("talk_list", " 呵呵  更新了");
            }

            @Override
            public void onNoChange() {
                Log.d("talk_list", " 呵呵  更新了，但是没变化 ");
            }

            @Override
            public void onAddMore(ArrayList<TalkListData> talkList) {
                talkListTempDatas.clear();
                talkListTempDatas.addAll(talkList);
                if (talkListTempDatas.size() > 0) {
                    mLlNoData.setVisibility(View.GONE);
                    mRlTalkList.setVisibility(View.VISIBLE);
                } else {
                    mLlNoData.setVisibility(View.VISIBLE);
                    mRlTalkList.setVisibility(View.GONE);
                }
                mTalkListAdapter.bindData(talkListTempDatas);
                Log.d("talk_list", " 呵呵  加载更多");
            }

            @Override
            public void onError(int errNum, String obj) {
                Log.d("UpDate_talk_list", " 呵呵  出错了 ");
                Toast.makeText(TalkListActivity.this, obj, Toast.LENGTH_LONG).show();
            }
        });


    }

    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            mSendMessage = mEtCommentEdit.getText().toString().trim();
            if (!StringUtil.isEmpty(mSendMessage)) {
                mTvSend.setVisibility(View.VISIBLE);
            } else {
                mTvSend.setVisibility(View.GONE);
            }

        }
    };

    private void initEmotion() {
        mEmotionUtil = new EmotionUtil(this, mEtCommentEdit, mLlEmotionDots, mVpEmotion);
        mEmotionUtil.initStaticFaces();
        mEmotionUtil.initViewPager();
        mVpEmotion.setOnPageChangeListener(mEmotionUtil.new PageChange());
    }


    @Override
    public void onClick(View view) {
        if (Constants.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.talk_list_back:
                finish();
                break;
            case R.id.talk_list_ll_help:
                Intent help = new Intent(mActivity, WebViewActivity.class);
                help.putExtra("title", "如何使用人脉圈?");
                help.putExtra("type", 14);
                startActivity(help);
                break;

            case R.id.iv_talk_list_emotion:
//                try {
////                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(TalkListActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                boolean visible = mEmotionLayout.getVisibility() == View.VISIBLE;
                if (visible) {
                    mIvEmotiom.setImageDrawable(getResources().getDrawable(R.drawable.selector_emotion_icon));
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

                } else {
                    mIvEmotiom.setImageDrawable(getResources().getDrawable(R.drawable.selector_keyboard_btn));
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(TalkListActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
                mEmotionLayout.setVisibility(visible ? View.GONE : View.VISIBLE);
                break;
            case R.id.talk_list_more:
                if (CONTACTS_CIRCLE.equals(getIntent().getAction())) {
                    //Bimp.deleteAllPhoto();
                    //PhotoUtil.deleteFolderFile(Environment.getExternalStorageDirectory()+"/Yun/Images/Photo",true);
                    Intent circle = new Intent(TalkListActivity.this, SendTalkActivity.class);
                    startActivity(circle);
                } else if (MY_PERSONAL_SPACE.equals(getIntent().getAction())) {
                    Intent mySpace = new Intent(TalkListActivity.this, MyTalkMessageActivity.class);
                    startActivity(mySpace);
                }

                break;
            case R.id.et_talk_list_input:
                visible = (mEmotionLayout.getVisibility() == View.VISIBLE);
                mIvEmotiom.setImageDrawable(getResources().getDrawable(R.drawable.selector_emotion_icon));
                if (visible) {
                    mEmotionLayout.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_list_new_aboutme:
                Intent intent = new Intent(TalkListActivity.this, MyTalkMessageActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_talk_list_send:
                if ("2".equals(mReplyMap.get(Constants.TALK_TYPE))) {
                    AddCommentMsg(mSendMessage, mReplyMap.get(Constants.TALK_ID), 2);
                } else {
                    AddCommentMsg(mSendMessage, mReplyMap.get(Constants.TALK_ID), 3);
                }
                break;
        }

    }

    private void initData(int pageIndex, boolean isUpdate) {
        talkListTempDatas.clear();
        talkListTempDatas.addAll(mCenter.getTalkData(pageIndex, isUpdate, mAction));
        mTalkListAdapter.bindData(talkListTempDatas);
        if (talkListTempDatas.size() > 0) {
            mLlNoData.setVisibility(View.GONE);
            mRlTalkList.setVisibility(View.VISIBLE);
        } else {
            mLlNoData.setVisibility(View.VISIBLE);
            mRlTalkList.setVisibility(View.GONE);
        }
    }

    private void getIntentData() {
        Intent intent = getIntent();
        mAction = intent.getAction();
        mTalkListAdapter = new TalkListAdapter(TalkListActivity.this,mAction);
        mLvTalkList.setAdapter(mTalkListAdapter);
        if (CONTACTS_CIRCLE.equals(mAction)) {
            mRightImg.setImageDrawable(getResources().getDrawable(R.drawable.selector_send_talk));
            mTvTitle.setText("人脉圈");
            initData(++pageIndex, true);
        } else if (MY_PERSONAL_SPACE.equals(mAction)) {
            mRightImg.setImageDrawable(getResources().getDrawable(R.drawable.selector_about_me));
            mTvTitle.setText("个人空间");
            initData(++pageIndex, true);
        } else if (OTHER_PERSONAL_SPACE.equals(mAction)) {
            mRightImg.setVisibility(View.GONE);
            mTvTitle.setText("个人空间");
            mCenter.deleteAllTalk(mAction);
            mCenter.setUsetId(intent.getStringExtra("set_user_id"));
            initData(++pageIndex, true);
            mIvNoData.setImageDrawable(getResources().getDrawable(R.drawable.talk_list_no_img1));
            mTvNoData.setText("他的空间里还没有内容");
        }
        if ("score".equals(intent.getStringExtra("type"))) {
            boolean isFirst = mPreferenceUtil.getBoolean("score_app_v1", true);
            if (isFirst) {
                showScoreDlg();
            }
        }

    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()) {
            case R.id.lv_talk_list:
                try {
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(TalkListActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                boolean visible = (mEmotionLayout.getVisibility() == View.VISIBLE);
                if (visible) {
                    mEmotionLayout.setVisibility(View.GONE);
                }
                mLlBottomLayout.setVisibility(View.GONE);

                break;

        }
        return false;
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        switch (absListView.getId()) {
            case R.id.lv_talk_list:
                if (is_scrolling && AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState) {
                    if (mTalkListAdapter.getCount() != Integer.parseInt(mPreferenceUtil.getString("talk_count", "0"))) {
                        initData(++pageIndex, false);
                    }else{
                        setFooterText(true);
                    }
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        switch (absListView.getId()) {
            case R.id.lv_talk_list:
                if ((firstVisibleItem + visibleItemCount == totalItemCount) && totalItemCount != 0) {
                    if (mTalkListAdapter.getCount() == Integer.parseInt(mPreferenceUtil.getString("talk_count", "0"))) {
                        setFooterText(true);
                    } else {
                        mFooterLayout.setVisibility(View.VISIBLE);
                    }
                    is_scrolling = true;
                } else {
                    is_scrolling = false;
                }
                break;
        }
    }



    /**
     * 弹出回复框
     *
     * @param replyMap
     * @param position
     */
    public void showBottomLayout(Map<String, String> replyMap, int position) {
        mLlBottomLayout.setVisibility(View.VISIBLE);
        boolean visible = (mEmotionLayout.getVisibility() == View.VISIBLE);
        if (visible) {
            mIvEmotiom.setImageDrawable(getResources().getDrawable(R.drawable.selector_emotion_icon));
            mEmotionLayout.setVisibility(View.GONE);
        }
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

        mPosition = position;
//        mReplyMap.clear();
        mReplyMap = new HashMap<String, String>();
        mReplyMap = replyMap;
        mEtCommentEdit.setText("");
        mEtCommentEdit.requestFocus();
        if ("2".equals(replyMap.get(Constants.TALK_TYPE))) {
            mEtCommentEdit.setHint("说点什么吧");
        } else {
            mEtCommentEdit.setHint("回复" + replyMap.get(Constants.TALK_REPLY_NAME));
        }
    }

    private void deleteTalk(String talkId) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_TALK_ID, talkId));
        GetMsgHttpReceiver message = new GetMsgHttpReceiver(mHandler.obtainMessage(DELETE_TALK));
        message.mApi = AsynHttpClient.API_TALK_DEL;
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execTalkHttp(message);
    }


    public void showDeleteTalkDlg(final String talkId) {
        String okText = this.getString(R.string.label_btn_delete);
        String cancelText = this.getString(R.string.cancel);
        mDialogUtil.titleDeleteMsgStyle(okText, cancelText, "删除此动态");
        mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
//                mWaitLayout.setVisibility(View.VISIBLE);
                httpReqDlgShow();
                deleteTalk(talkId);
                lDialog.dismiss();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();
            }
        });
    }

    private void GetNewMessage() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        String sid = LocalLogin.getInstance().mSid;
        if (sid.equals(LocalLogin.VISITOR_SID)){
            LocalLogin.getInstance().login(this);
            sid = LocalLogin.getInstance().mSid;
        }
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, sid));
        GetTalkNewMsg message = new GetTalkNewMsg(mHandler.obtainMessage(GET_TALK_NEW_MSG_COUNT), this);
        message.mApi = AsynHttpClient.API_TALK_NEW_MSG_COUNT;
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execTalkHttp(message);

        Log.d("onInstance", "GetNewMessage  === " + LocalLogin.getInstance().mSid);
    }

    public void setFooterText(boolean isFinish) {
        if (isFinish) {
            mFooterLayout.setVisibility(View.GONE);
        } else {
            mFooterText.setText(this.getResources().getString(
                    R.string.xlistview_footer_hint_normal));
            mFooterProgress.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 显示等待的dialog
     */
    private void httpReqDlgShow() {
        if (mWaitDialog == null) {
            mWaitDialog = AllDialogUtil.getInstance(mActivity).waitDialog();
//            mProgressDialog.setCancelable(false);
            mWaitDialog.show();
        }

    }

    /**
     * 取消等待的dialog
     */
    private void httpReqDlgDismiss() {
        if (mWaitDialog != null && mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
            mWaitDialog = null;
        }
    }

    /**
     * 增加评论
     *
     * @param content
     * @param tagId
     * @param type    2：直接回复    3：回复某一个人
     */
    private void AddCommentMsg(String content, String tagId, int type) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_TALK_ID, tagId));
        AddTalkCommentMsg message = new AddTalkCommentMsg(mHandler.obtainMessage(ADD_COMMENT), this);
        if (type == 2) {
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_CONTENT, content));
            message.mApi = AsynHttpClient.API_TALK_ADD_COMMENT;
        } else {
            message.mApi = AsynHttpClient.API_TALK_REPLY;
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_REPLY_UID, mReplyMap.get(Constants.TALK_REPLY_ID)));
            nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_REPLY_TXT, content));
        }
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execTalkHttp(message);
    }

    /**
     * 评分弹框
     */
    public void showScoreDlg() {
        AllDialogUtil dialogUtil = AllDialogUtil.getInstance(TalkListActivity.this);
        String okText = "评分";
        String cancelText = "下次";
        dialogUtil.titleMsgBtnStyle("", "感谢你对《团队》的支持！如果觉的《团队》还不错的话，就去给个好评吧！", okText, cancelText);
        dialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
                mPreferenceUtil.save("score_app_v1", false);

                try{
                    Uri uri = Uri.parse("market://details?id="+getPackageName());
                    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }catch(ActivityNotFoundException e){
                    Toast.makeText(TalkListActivity.this, "Couldn't launch the market !", Toast.LENGTH_SHORT).show();
                }
                lDialog.dismiss();

            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();
                mPreferenceUtil.save("score_app_v1", false);

            }

        });

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        LocalLogin.getInstance().login(this);
        Log.d("onInstance", "onSaveInstanceState  === " + LocalLogin.getInstance().mSid);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        if (LocalLogin.getInstance().mSid.equals(LocalLogin.VISITOR_SID)){
            LocalLogin.getInstance().login(this);
        }
        Log.d("onInstance", "onRestoreInstanceState  === " + LocalLogin.getInstance().mSid);
        super.onRestoreInstanceState(savedInstanceState);
    }

}
